package com.kldy8.kldyw.filmfactory.company;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kldy8.kldyw.MyWebActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.filmfactory.bean.Company;
import com.kldy8.kldyw.system.UrlBuilder;
import com.kldy8.kldyw.tools.widget.CircleImageView;
import com.kldy8.kldyw.web.AppWebUrl;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import java.util.List;


/**
 * 人才
 * Created by Administrator on 2016/9/5.
 */
public class CompanyAdapter extends BaseAdapter
{
    ImageLoader loader;

    private List<Company> listItems;
    private Context context;                        //运行上下文
    private LayoutInflater listContainer;           //视图容器
    private boolean[] hasChecked;
    private int myType;


    //记录商品选中状态
    public final class ListItemView
    {                //自定义控件集合
        public CircleImageView image;
        public TextView title;
        public TextView info;
        public Button left_text_button;
        public Button right_text_button;
    }

    public void replaceOrAdd(boolean isInit, List<Company> listItems)
    {
        if (isInit)
        {
            this.listItems = listItems;
        }
        else
        {
            if (this.listItems != null)
            {
                if (listItems != null)
                {
                    this.listItems.addAll(listItems);
                }
            }
            else
            {
                this.listItems = listItems;
            }
        }
    }

    DisplayImageOptions options;

    public CompanyAdapter(Context context, List<Company> filmTalents, int type)
    {
        this.context = context;
        this.listItems = filmTalents;
        listContainer = LayoutInflater.from(context);   //创建视图容器并设置上下文
        hasChecked = new boolean[getCount()];
        myType = type;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_empty)//设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_empty)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_empty)//设置图片加载/解码过程中错误时候显示的图片
//                .delayBeforeLoading(1000)//设置延时多少时间后开始下载
//                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的资源是否缓存在SD卡中
//                .considerExifParams(true)// 是否考虑JPEG图像EXIF参数（旋转，翻转）
//                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)//设置图片以何种编码方式显示
//                .bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少
//                .displayer(new FadeInBitmapDisplayer(1000))//是否图片加载好后渐入的动画时间
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480, 800)//内存缓存文件的最大长宽
                .discCacheExtraOptions(480, 800, null)//本地缓存的详细信息
                .threadPoolSize(3)//线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY-2)//设置当前线程的优先级
                .tasksProcessingOrder(QueueProcessingType.FIFO)//设置任务的处理顺序
                .denyCacheImageMultipleSizesInMemory()//防止内存中图片重复
                .memoryCache(new LruMemoryCache(2*1024*1024))//设置自己的内存缓存大小   2M
                .memoryCacheSize(2*1024*1024)
                .memoryCacheSizePercentage(13)//内存缓存百分比
                .imageDownloader(new BaseImageDownloader(context))
                .imageDecoder(new BaseImageDecoder(true))//图片解码
                .defaultDisplayImageOptions(options)//是否使用默认的图片加载配置，null表示不使用
                .build();

        loader = ImageLoader.getInstance();
        loader.init(config);


    }

    public int getCount()
    {
        // TODO Auto-generated method stub
        if (null == listItems )
        {
             return 1;
        }
        else
        {
            return listItems.size();
        }
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    /**
     * 记录勾选了哪个物品
     * @param checkedID 选中的物品序号
     */
    private void checkedChange(int checkedID) {
        hasChecked[checkedID] = !hasChecked[checkedID];
    }

    /**
     * 判断物品是否选择
     * @param checkedID 物品序号
     * @return 返回是否选中状态
     */
    public boolean hasChecked(int checkedID) {
        return hasChecked[checkedID];
    }



    /**
     * ListView Item设置
     */
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        // TODO Auto-generated method stub
        final int selectID = position;
        //自定义视图
        ListItemView  listItemView = null;
        if (null == listItems || listItems.size() == 0)
        {
            convertView = listContainer.inflate(R.layout.view_notfount, null);
            return convertView;
        }
        else{
        if (convertView == null)
        {

            listItemView = new ListItemView();
            //获取list_item布局文件的视图
            if (myType == 0)
            {
                convertView = listContainer.inflate(R.layout.company_item, null);
                listItemView.image = (CircleImageView) convertView.findViewById(R.id.image);
            }
            else
            {
                convertView = listContainer.inflate(R.layout.company_item, null);
            }
            //获取控件对象
            listItemView.title = (TextView)convertView.findViewById(R.id.title);
            listItemView.info = (TextView)convertView.findViewById(R.id.supportingText);
            listItemView.left_text_button= (Button)convertView.findViewById(R.id.left_text_button);
            listItemView.right_text_button = (Button)convertView.findViewById(R.id.right_text_button);
            //设置控件集到convertView
            convertView.setTag(listItemView);
        }else {
            listItemView = (ListItemView)convertView.getTag();
        }

          Company  company = listItems.get(position);
         String headUrl = company.logo;

        if (myType == 0)
        {
            loader.displayImage(headUrl, listItemView.image,options);
        }
        listItemView.title.setText(company.name);
        listItemView.info.setText(company.description);
        listItemView.left_text_button.setText(company.city);

        convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Company  acompany = listItems.get(position);
                Intent intent = new Intent();
                String resultUrl = AppWebUrl.createUrl(acompany.url);
                intent.putExtra("url",resultUrl);
                intent.setClass(context, MyWebActivity.class);
                ((Activity)context).startActivity(intent);
                ((Activity)context).overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
            }
        });

        return convertView;
    }
    }
}